package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlScorm entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlScorm
 * @author MyEclipse Persistence Tools
 */

public class MdlScormDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MdlScormDAO.class);
	// property constants
	public static final String COURSE = "course";
	public static final String NAME = "name";
	public static final String SCORMTYPE = "scormtype";
	public static final String REFERENCE = "reference";
	public static final String INTRO = "intro";
	public static final String INTROFORMAT = "introformat";
	public static final String VERSION = "version";
	public static final String MAXGRADE = "maxgrade";
	public static final String GRADEMETHOD = "grademethod";
	public static final String WHATGRADE = "whatgrade";
	public static final String MAXATTEMPT = "maxattempt";
	public static final String FORCECOMPLETED = "forcecompleted";
	public static final String FORCENEWATTEMPT = "forcenewattempt";
	public static final String LASTATTEMPTLOCK = "lastattemptlock";
	public static final String DISPLAYATTEMPTSTATUS = "displayattemptstatus";
	public static final String DISPLAYCOURSESTRUCTURE = "displaycoursestructure";
	public static final String UPDATEFREQ = "updatefreq";
	public static final String SHA1HASH = "sha1hash";
	public static final String MD5HASH = "md5hash";
	public static final String REVISION = "revision";
	public static final String LAUNCH = "launch";
	public static final String SKIPVIEW = "skipview";
	public static final String HIDEBROWSE = "hidebrowse";
	public static final String HIDETOC = "hidetoc";
	public static final String HIDENAV = "hidenav";
	public static final String AUTO = "auto";
	public static final String POPUP = "popup";
	public static final String OPTIONS = "options";
	public static final String WIDTH = "width";
	public static final String HEIGHT = "height";
	public static final String TIMEOPEN = "timeopen";
	public static final String TIMECLOSE = "timeclose";
	public static final String TIMEMODIFIED = "timemodified";

	public void save(MdlScorm transientInstance) {
		log.debug("saving MdlScorm instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(MdlScorm persistentInstance) {
		log.debug("deleting MdlScorm instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlScorm findById(java.lang.Long id) {
		log.debug("getting MdlScorm instance with id: " + id);
		try {
			MdlScorm instance = (MdlScorm) getSession().get(
					"idv.loveloser.dao.MdlScorm", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlScorm instance) {
		log.debug("finding MdlScorm instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlScorm")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MdlScorm instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MdlScorm as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCourse(Object course) {
		return findByProperty(COURSE, course);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByScormtype(Object scormtype) {
		return findByProperty(SCORMTYPE, scormtype);
	}

	public List findByReference(Object reference) {
		return findByProperty(REFERENCE, reference);
	}

	public List findByIntro(Object intro) {
		return findByProperty(INTRO, intro);
	}

	public List findByIntroformat(Object introformat) {
		return findByProperty(INTROFORMAT, introformat);
	}

	public List findByVersion(Object version) {
		return findByProperty(VERSION, version);
	}

	public List findByMaxgrade(Object maxgrade) {
		return findByProperty(MAXGRADE, maxgrade);
	}

	public List findByGrademethod(Object grademethod) {
		return findByProperty(GRADEMETHOD, grademethod);
	}

	public List findByWhatgrade(Object whatgrade) {
		return findByProperty(WHATGRADE, whatgrade);
	}

	public List findByMaxattempt(Object maxattempt) {
		return findByProperty(MAXATTEMPT, maxattempt);
	}

	public List findByForcecompleted(Object forcecompleted) {
		return findByProperty(FORCECOMPLETED, forcecompleted);
	}

	public List findByForcenewattempt(Object forcenewattempt) {
		return findByProperty(FORCENEWATTEMPT, forcenewattempt);
	}

	public List findByLastattemptlock(Object lastattemptlock) {
		return findByProperty(LASTATTEMPTLOCK, lastattemptlock);
	}

	public List findByDisplayattemptstatus(Object displayattemptstatus) {
		return findByProperty(DISPLAYATTEMPTSTATUS, displayattemptstatus);
	}

	public List findByDisplaycoursestructure(Object displaycoursestructure) {
		return findByProperty(DISPLAYCOURSESTRUCTURE, displaycoursestructure);
	}

	public List findByUpdatefreq(Object updatefreq) {
		return findByProperty(UPDATEFREQ, updatefreq);
	}

	public List findBySha1hash(Object sha1hash) {
		return findByProperty(SHA1HASH, sha1hash);
	}

	public List findByMd5hash(Object md5hash) {
		return findByProperty(MD5HASH, md5hash);
	}

	public List findByRevision(Object revision) {
		return findByProperty(REVISION, revision);
	}

	public List findByLaunch(Object launch) {
		return findByProperty(LAUNCH, launch);
	}

	public List findBySkipview(Object skipview) {
		return findByProperty(SKIPVIEW, skipview);
	}

	public List findByHidebrowse(Object hidebrowse) {
		return findByProperty(HIDEBROWSE, hidebrowse);
	}

	public List findByHidetoc(Object hidetoc) {
		return findByProperty(HIDETOC, hidetoc);
	}

	public List findByHidenav(Object hidenav) {
		return findByProperty(HIDENAV, hidenav);
	}

	public List findByAuto(Object auto) {
		return findByProperty(AUTO, auto);
	}

	public List findByPopup(Object popup) {
		return findByProperty(POPUP, popup);
	}

	public List findByOptions(Object options) {
		return findByProperty(OPTIONS, options);
	}

	public List findByWidth(Object width) {
		return findByProperty(WIDTH, width);
	}

	public List findByHeight(Object height) {
		return findByProperty(HEIGHT, height);
	}

	public List findByTimeopen(Object timeopen) {
		return findByProperty(TIMEOPEN, timeopen);
	}

	public List findByTimeclose(Object timeclose) {
		return findByProperty(TIMECLOSE, timeclose);
	}

	public List findByTimemodified(Object timemodified) {
		return findByProperty(TIMEMODIFIED, timemodified);
	}

	public List findAll() {
		log.debug("finding all MdlScorm instances");
		try {
			String queryString = "from MdlScorm";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MdlScorm merge(MdlScorm detachedInstance) {
		log.debug("merging MdlScorm instance");
		try {
			MdlScorm result = (MdlScorm) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MdlScorm instance) {
		log.debug("attaching dirty MdlScorm instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MdlScorm instance) {
		log.debug("attaching clean MdlScorm instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}